import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:initproject/demos/led/LEDItem.dart';
import 'package:mybaseplus/base/tool/globalMethod/FFScript.dart';
import 'package:mybaseplus/base/tool/pprint/FFLog.dart';
import 'package:mybaseplus/base/widget/layout/FFContainer.dart';
import 'package:mybaseplus/base/widget/layout/FFStackContainer.dart';

/// 滚动方向，向左，向右
/// 文本方向，竖向，横向
/// 字体给三档，小、中、大
/// 速度给三档，稍慢，适中，稍快
/// 字体颜色任何设定，
/// 背景颜色任意设定，
/// 背景图片设定，
///
/// 支持颜色渐变动画，
/// 背景音乐，
///
/// 文本颗粒花纹
///
/// 支持内容多元化，（例如，文本和图片可交插滚动）
///
/// 导出gif、视频、壁纸
///
/// 广告+订阅模式
///

class LEDWidget extends StatefulWidget {
  ///是否重复
  bool? isRepeat;
  ///文本参数
  List<LEDWidgetParam> param;

  LEDWidget({
    Key? key,
    this.isRepeat = true,
    required this.param,
  }) : super(key: key);

  @override
  State<LEDWidget> createState() => _LEDWidgetState();
}

class _LEDWidgetState extends State<LEDWidget> implements ILEDItemDelegate {
  List<Widget> list = [];
  int nameIndex = 0;

  @override
  void initState() {
    super.initState();
    load();
  }

  ///加载第一个LED
  load() {
    list.add(LEDItem(
      key: GlobalKey(),
      name: (nameIndex++).toString(),
      param: widget.param.first,
      delegate: this,
    ));

  }

  ///创建一个新的LED
  @override
  delegateCreateNextLED() async {
    if (widget.isRepeat != true) {
      return;
    }

    list.add(LEDItem(
        key: GlobalKey(),
        name: (nameIndex++).toString(),
        param: widget.param[nameIndex % widget.param.length],
        delegate: this));

    setState(() {});
  }

  ///删除指定的LED
  @override
  delegateDeleteCurrentLED(LEDItem item) {
    list.remove(item);
    if (widget.isRepeat != true) {
      FFLog.info("结束了，不重复");
      return;
    }
  }

  @override
  Widget build(BuildContext context) {
    return FFContainer(
      color: Colors.white,
      child: FFStackContainer(
        children: globalScript(() {
          List<Widget> children = [
            FFContainer(
              child: null,
            ),
          ];

          for (var element in list) {
            children.add(element);
          }

          return children;
        }),
      ),
    );
  }
}

class LEDWidgetParam {
  String value;
  LEDType? type;
  TextStyle? textStyle;
  double? speed; // 取值范围：0~1

  LEDWidgetParam({
    required this.value,
    this.type = LEDType.text,
    this.speed = 0.5,
    this.textStyle,
  });
}

///展示的类型
enum LEDType{
  text, //文本
  img,  //图片
}
